home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / dbf_tc.zip / D_CPYSTR.C < prev    next >
Text File  |  1987-06-19  |  2KB  |  73 lines

  1. /* 
  2. **        file:        d_cpystr.c
  3. **        purpose:    routine to create a new dbiii file useing the structure of an
  4. **                    existing file.
  5. **        ussage:    source = (struct DBF *)malloc(sizeof(struct DBF));
  6. **                    dest    = (struct DBF *)malloc(sizeof(struct DBF));
  7. **                    strcpy(source->filename,"source.dbf");
  8. **                    strcpy(dest->filename,"dest.dbf");
  9. **                    d_open(source);
  10. **                    d_cpystr(source,dest);
  11. **                    d_close(source);
  12. **                    d_close(dest);
  13. **                    free(source);
  14. **                    free(dest);
  15. **        notes:    compile with "tcc -c d_cpystr".    include this file in dbf.lib
  16. **                    this code fragment does the same thing as:
  17. **                    USE SOURCE
  18. **                    COPY STRUCTURE TO DEST
  19. **        returns:    0                if successful with structure filled in
  20. **                    NO_FILE        if unable to find file
  21. **                    OUT_OF_MEM    if not enough memory
  22. **        author:    Mark Sadler
  23. **        revised:    6/19/87
  24. */
  25.  
  26. #include <stdio.h>
  27. #include <alloc.h>
  28. #include "dbf.h"
  29.  
  30. int d_cpystr(struct DBF *s,struct DBF *d)
  31. {
  32.     struct FIELD_RECORD *f;
  33.     int i;
  34.     int n;
  35.  
  36.     /* make sure source file is open */
  37.     if(s->status != not_updated && s->status != updated)
  38.         return(NO_FILE);
  39.  
  40.     d->status = not_open;                        /* in case can not open */
  41.  
  42.     if((d->file_ptr = fopen(d->filename,"w+b")) == NULL)
  43.         return(NO_FILE);
  44.     
  45.     if((d->fields_ptr = (struct FIELD_RECORD *)malloc(s->num_fields * FIELD_REC_LEN))==NULL)
  46.         return(OUT_OF_MEM);
  47.     
  48.     memcpy(d->fields_ptr,s->fields_ptr,(s->num_fields * FIELD_REC_LEN));
  49.     memcpy(&d->dbf_version,&s->dbf_version,12);
  50.     d->records = 0L;
  51.     d->current_record = 0;
  52.     d->num_fields = s->num_fields;
  53.     /* the following line forces the copied dbf structure to be in dbIII+ format */
  54.     /* dbIII+ files can be read without problems with dbIII. */
  55.     d->header_length=HEADER_PROLOG+(d->num_fields * FIELD_REC_LEN)+1;
  56.     if((d->record_ptr = (char *)malloc(d->record_length))==NULL)
  57.         return(OUT_OF_MEM);
  58.     /* initialize pointers to fields in record */
  59.     for(i=0,n=1;i<d->num_fields;i++)
  60.     {
  61.         f = d->fields_ptr + i;
  62.         f->field_data_address = d->record_ptr + n;
  63.         n += f->len;
  64.     }
  65.     fwrite(&d->dbf_version,1,12,d->file_ptr);
  66.     for(i=1;i<=20;i++)
  67.     fwrite("\0",1,1,d->file_ptr);
  68.     fwrite(d->fields_ptr,FIELD_REC_LEN,d->num_fields,d->file_ptr);
  69.     fwrite("\x0d",1,1,d->file_ptr);
  70.     d->status = updated;                        /* open successfull */
  71.     return(0);
  72. }
  73.